package com.example.controller;

import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String login() {
        return "login";
    }
    @GetMapping("main")
    public String main(){
        return "main";
    }
    @PostMapping("/logincheck")
    public String logincheck(@RequestParam("username") String username,
                             @RequestParam("password") String password,
                             Map<String, Object> map,
                             HttpSession session){
        String userNick = userService.userLogin(username, password);
        if (userNick != null){
            session.setAttribute("user", userNick);
            return "redirect:/main";
        }else {
            map.put("msg", "用户名或密码错误");
            return "login";
        }
    }

}
